package com.kuqi.controller;

import com.kuqi.dao.TestRepository;
import com.kuqi.entity.TestModel;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Optional;

@Controller
public class TestController {

    @Autowired
    TestRepository testRepository;

    //查询全部数据
    @ResponseBody
    @RequestMapping(value = "/app/findAll.html", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String findAll(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        //构造默认反馈
        String responseStr = new TestModel(-1,"未查找到数据",null, 0, 0).toJsonString();
        //查找数据并作为json语句返回
        List<TestModel> list = testRepository.findAll();
        if(list.size() >0){
            JSONObject result = new JSONObject();
            JSONArray arr = new JSONArray();
            for (TestModel item : list) {
                arr.add(item.toJsonObject());
            }
            result.put("code",1);
            result.put("msg","查询成功");
            result.put("data",arr);
            responseStr = result.toJSONString();
        }
        return responseStr;
    }


    // 根据id查询部分数据
    @ResponseBody
    @RequestMapping(value = "/app/findById.html", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String findById(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        //构造默认反馈
        String responseStr = new TestModel(-1,"未查找到数据",null, 0, 0).toJsonString();
        //查找数据并作为json语句返回
        int id = Integer.parseInt(jsonParam.get("id").toString());
        Optional<TestModel> optional = testRepository.findById(id);
        if(optional.isPresent()) {
            TestModel res = optional.get();
            JSONObject result = new JSONObject();
            JSONArray arr = new JSONArray();
            arr.add(res.toJsonObject());
            result.put("code",1);
            result.put("msg","查询成功");
            result.put("data",arr);
            responseStr = result.toJSONString();
        }
        return responseStr;
    }

    // 根据name查询部分数据（sql）
    @ResponseBody
    @RequestMapping(value = "/app/findByName.html", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String findByName(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        //构造默认反馈
        String responseStr = new TestModel(-1,"未查找到数据",null, 0, 0).toJsonString();
        //查找数据并作为json语句返回
        String name = jsonParam.get("name").toString();
        List<TestModel> list = testRepository.findByName(name);
        if(list.size() >0){
            JSONObject result = new JSONObject();
            JSONArray arr = new JSONArray();
            for (TestModel item : list) {
                arr.add(item.toJsonObject());
            }
            result.put("code",1);
            result.put("msg","查询成功");
            result.put("data",arr);
            responseStr = result.toJSONString();
        }
        return responseStr;
    }


    // 根据某两列条件查询数据
    @ResponseBody
    @RequestMapping(value = "/app/findByHeightAndWeight.html", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String findByHeightAndWeight(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        //构造默认反馈
        String responseStr = new TestModel(-1,"未查找到数据",null, 0, 0).toJsonString();
        //查找数据并作为json语句返回
        int height = Integer.parseInt(jsonParam.get("height").toString());
        int weight = Integer.parseInt(jsonParam.get("weight").toString());
        List<TestModel> list = testRepository.findByHeightAndWeight(height, weight);
        if(list.size() >0){
            JSONObject result = new JSONObject();
            JSONArray arr = new JSONArray();
            for (TestModel item : list) {
                arr.add(item.toJsonObject());
            }
            result.put("code",1);
            result.put("msg","查询成功");
            result.put("data",arr);
            responseStr = result.toJSONString();
        }
        return responseStr;
    }


    // 新增一组数据
    @ResponseBody
    @RequestMapping(value = "/app/insertValue.html", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String insertValue(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        //构造默认反馈
        String responseStr = new TestModel(-1,"执行失败！",null, 0, 0).toJsonString();
        //获取参数并执行
        int id = Integer.parseInt(jsonParam.get("id").toString());
        String name = jsonParam.get("name").toString();
        String sex = jsonParam.get("sex").toString();
        int res = testRepository.insertValue(id, name, sex);
        if(res >0){
            JSONObject result = new JSONObject();
            result.put("code",1);
            result.put("msg","新增成功");
            result.put("data",null);
            responseStr = result.toJSONString();
        }
        return responseStr;
    }


    // 条件删除一组数据
    @ResponseBody
    @RequestMapping(value = "/app/deleteByName.html", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String deleteByName(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        //构造默认反馈
        String responseStr = new TestModel(-1,"执行失败！",null, 0, 0).toJsonString();
        //获取参数并执行
        String name = jsonParam.get("name").toString();
        int res = testRepository.deleteByName(name);
        if(res >0){
            JSONObject result = new JSONObject();
            result.put("code",1);
            result.put("msg","删除成功");
            result.put("data",null);
            responseStr = result.toJSONString();
        }
        return responseStr;
    }


    // 条件修改一组数据
    @ResponseBody
    @RequestMapping(value = "/app/updateByName.html", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String updateByName(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        //构造默认反馈
        String responseStr = new TestModel(-1,"执行失败！",null, 0, 0).toJsonString();
        //获取参数并执行
        String name = jsonParam.get("name").toString();
        int height = Integer.parseInt(jsonParam.get("height").toString());
        int res = testRepository.updateByName(height, name);
        if(res >0){
            JSONObject result = new JSONObject();
            result.put("code",1);
            result.put("msg","修改成功");
            result.put("data",null);
            responseStr = result.toJSONString();
        }
        return responseStr;
    }








    //直接反馈一句话，不需要参数
    @ResponseBody
    @RequestMapping(value = "/app/testWorld.html")
    public String TestWorldFunc() {
        return "IU90D76-6635MNF4J-277KJY5Q9DG";
    }


    // 直接反馈一个完整的页面
    @RequestMapping(value = "/app/testHtml.html")
    public String TestHtmlFunc() {
        return "Hello.html";
    }

    // 直接反馈一个完整的页面(带配置)
    @RequestMapping(value = "/app/testComputerHtml.html")
    public String TestComputerHtmlFunc() {
        return "demoPage.html";
    }

    // 直接反馈一个完整的页面(反馈一个游戏！)
    @RequestMapping(value = "/app/testGameHtml.html")
    public String TestGameHtmlHtmlFunc() {
        return "index.html";
    }

    // 进入注册页面
    @RequestMapping(value = "/app/Register.html")
    public String TestHtmlRegister() {
        return "Register.html";
    }


    // 测试是否可以登录
    @ResponseBody
    @RequestMapping(value = "/app/CONNECT.html")
    public String TestCanLoad() {
        System.out.println("成功连接到服务器！");
        return "true";
    }


}